<?
if ($this->entry->GetId())
    $title = 'Редактирование статьи';
else
    $title = 'Добавление статьи';   
?>
<h1><?=$title;?></h1>
<form id="articleForm" action="<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => $this->ctrlName, 'action' => 'save_article'));?>"
	method="post" enctype="multipart/form-data">
<table align="center" class="formTable">
	<tr>
		<td class="field-title"><label>Дата публикации</label></td>
		<td class="field-place"><input type="text" id="publish_date" name="publish_date" value="<?=htmlspecialchars($this->entry->GetPublishDate());?>"></td>
	</tr>
	<tr>
		<td class="field-title"><label>Заголовок</label></td>
		<td class="field-place"><input class="input-wide" name="title" type="text" id="articleName" value="<?=htmlspecialchars($this->entry->GetTitle());?>"></td>
	</tr>
<?php 
if (!$this->blog->GetId())
{
?>	
	<tr>
		<td class="field-title"><label>Url</label></td>
		<td class="field-place"><input type="text"  id="articleUrl" name="url" value="<?=htmlspecialchars($this->entry->GetUrl());?>"><span class="alarm" title="допустимые символы: a-z,-,_,0-9">!</span></td>
	</tr>	
	<tr>
		<td class="field-title"><label>Топ-новость</label></td>
		<td class="field-place"><input type="checkbox" name="is_main" value="1"<?=($this->entry->GetIsMain() ? ' checked' : '');?>></td>
	</tr>
<?php 
}
?>	
	<tr>
		<td class="field-title"><label>Тэги</label></td>
		<td class="field-place"><div id="articleTagSuggest" class="tagBox">
				<input name="tags" id="articleTags" class="tagBoxInput input-wide" type="text" value="<?=htmlspecialchars($this->entry->GetTags());?>">
			</div>
		</td>
	</tr>
	<tr>
		<td><label>Основной медиа файл</label></td>
		<td>
			<div id="articleMediaThumb"></div>
		</td>
	</tr>							
</table>

<?php 
if (!$this->blog->GetId())
{
?>
<div class="editBlock">
<h2>Категории</h2>
<table width="100%">
<?php 

	$articeCats = $this->entry->GetCategories();

	if (!$this->entry->GetId())
	{
		$articeCats[] = $this->filter->GetCategory();
	}	
	
	foreach ($this->newsCategoryIds as $catId)
	{
		$cat = $this->structMapper->GetById($catId);
		if (!$cat) continue;
?>
<tr>
	<td width="1%"><input id="article_cat_<?php echo $cat->GetId(); ?>" type="checkbox" name="category[]" value="<?php echo $cat->GetId(); ?>"<?php echo in_array($cat->GetId(), $articeCats) ? ' checked="checked"' : ''; ?>></td>	
	<td>
		<label for="article_cat_<?php echo $cat->GetId(); ?>" class="label-left"><small><?php
		$parentCatIds = $this->structMapper->GetParentElementsIds($cat->GetId());
		foreach ($parentCatIds as $pCatId)
		{
			$pCat = $this->structMapper->GetById($pCatId);	
			echo $pCat->GetName(). ' &gt; ';
		}
		?></small>
		<?php echo $cat->GetName(); ?></label>	
	</td>
</tr>
<?php	
	}
?>
</table>
</div>
<?php 
}
?>

<div class="editBlock">
	<h2>Анонс</h2>
	<label for="articleBrief" class="editor-init-button editorInitButton">редактор</label>
	<textarea id="articleBrief" class="forMediaHTML" style="width: 100%" name="brief" rows="5"><?=htmlspecialchars($this->entry->GetBrief());?></textarea>
</div>


<div class="editBlock">
	<h2>Статья</h2>
	<label for="articleContent" class="editor-init-button editorInitButton">редактор</label>
	<textarea id="articleContent" class="forMediaHTML" style="width: 100%" name="content" rows="15"><?=htmlspecialchars($this->entry->GetContent());?></textarea>
</div>

<table>
	<tr>
		<td class="r"><input type="submit" value="Сохранить" class="bt" /></td>
		<td class="l"><input type="button" value="Назад" class="bt"
			onClick="window.location.href='<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => $this->ctrlName));?>';" /></td>
	</tr>
</table>
<input type="hidden" name="blog" value="<?=$this->blog->GetId();?>">
<input type="hidden" name="id" value="<?=$this->entry->GetId();?>">
</form>
<div style="display: none;" id="mediaArticleTemplate">
	<img src="" align="absmiddle" class="thumb-img"><a href="#" class="del-article-media-link">
	<img src="<?=_ROOT_URL;?>files/images/admin/edit_close.gif" alt="delete" title="" width="9" height="11" align="absmiddle"></a>
	<input type="hidden" name="media_file_id" value="">
</div>
<div style="display: none;" id="mediaArticleNone">
	Выберите файл, нажав <img src="<?=_ROOT_URL;?>files/images/admin/icons/add_16x16.png" alt="add" title="" width="16" height="16" align="absmiddle">
	в списке медиа файлов в левой панели.
</div>

<?=$this->Style()->IncludeStyleFile(array('js' => array('ckeditor')));?>
<?=$this->Style()->IncludeStyleFile(array('js' => array('tagdragon')));?>
<script type="text/javascript">

$(document).ready(function(){
	$('#articleName').focus(function(){
		var articleUrl = $('#articleUrl');	
		if (articleUrl && (articleUrl.val() == ''))
		{
			$(this).syncTranslit({destination: 'articleUrl'});
		}	
		else
		{
			$(this).unbind('keyup');
		}	
	});
	
	setArticleMediaFile('<?php echo $this->entry->GetMediaFile()->GetId(); ?>', '<?=_ROOT_URL.$this->thumbFolder.$this->entry->GetMediaFile()->GetThumbnail();?>');		
});

$(document).ready(function() 
{
	$('#articleTagSuggest').tagdragon({'field':'articleTags',
										 'max':10,
										 'charMin':2,
										 'tagsep':',',
										 'url':'<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'articles', 'action' => 'tags_list', 'blog' => $this->blog->GetId()));?>'});
});

function addArticleMainMediaFile(id)
{
    $.getJSON(
			'<?=MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => 'media', 'action' => 'get_media_file'));?>',
			{
				'id': id
			},
			function(data){
					if (0 != data.status)
					{
						alert(data.message);
						return false;
					}

					var mediaObj = data.data.value;
					setArticleMediaFile(mediaObj.id, '<?=_ROOT_URL.$this->thumbFolder;?>'+mediaObj.thumbnail);		
			}
		);		
}

function setArticleMediaFile(id, src)
{
	if (!id || !src)
	{
		$('#articleMediaThumb').html($('#mediaArticleNone').html());
		return;
	}		

	var html = $('<div></div>').html($('#mediaArticleTemplate').html());

	html.find('.thumb-img').attr('src', src);
	html.find('.del-article-media-link').click(function(){
		setArticleMediaFile();
	});
	html.find(':input[name=media_file_id]').val(id);
	
	$('#articleMediaThumb').html(html);
}
</script>